#ifndef __TCPCONNECTION_H__
#define __TCPCONNECTION_H__

#include "socket.h"
#include "socketIO.h"
#include "inetAddress.h"
#include <functional>
#include <memory>

using std::function;
using std::shared_ptr;

class EventLoop;

class TcpConnection
:public std::enable_shared_from_this<TcpConnection>
{
public:
	explicit TcpConnection(int fd,EventLoop * loop);

	~TcpConnection();

	void send(const string & msg);

	void sendInLoop(const string & msg);

	string receive();

	string toString();

	bool isClose();

	void setNewConnectionCallBack(const function<void(const shared_ptr<TcpConnection>&)>&callBack);
	void setMessageCallBack(const function<void(const shared_ptr<TcpConnection>&)>&callBack);
	void setCloseCallBack(const function<void(const shared_ptr<TcpConnection>&)>&callBack);

	void handleNewConnectionCallBack();
	void handleMessageCallBack();
	void handleCloseCallBack();

private:
	InetAddress getLocalAddr();

	InetAddress getPeerAddr();

	EventLoop * _loop;

	SocketIO _socketIO;

	Socket _socket;
	
	InetAddress _localAddr;

	InetAddress _peerAddr;

	function<void(const shared_ptr<TcpConnection>&)> _callBackNewConnection;
	function<void(const shared_ptr<TcpConnection>&)> _callBackMessage;
	function<void(const shared_ptr<TcpConnection>&)> _callBackClose;


};

#endif
